package com.fm.webrtc.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fm.webrtc.R;
import com.fm.webrtc.modle.StreamModel;

import org.webrtc.AudioTrack;
import org.webrtc.EglBase;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoSink;
import org.webrtc.VideoTrack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VideoRecyclerAdapter extends RecyclerView.Adapter<VideoRecyclerAdapter.VideoViewHolder> {


    private final static String TAG = VideoRecyclerAdapter.class.getSimpleName();

    private Context context;

    private EglBase.Context eglBaseContext;

    public List<StreamModel> getStreamModelList() {
        return streamModelList;
    }

    public List<StreamModel> streamModelList = new ArrayList<>();


    private HashMap<String, VideoSink> videoSinkHashMap = new HashMap<>();


    public VideoRecyclerAdapter(Context context, EglBase.Context eglBaseContext) {
        this.context = context;
        this.eglBaseContext = eglBaseContext;
    }

    public void addVideoTracks(List<StreamModel> streamModelList) {
        this.streamModelList = streamModelList;
//        videoSinkHashMap = new HashMap<>();
        Log.e("videoTracks", String.valueOf(this.streamModelList.size()));
        notifyDataSetChanged();
    }



    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        return new VideoViewHolder(LayoutInflater.from(context).inflate(R.layout.video_item, viewGroup, false));

    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        try{
            StreamModel streamModel = this.streamModelList.get(position);
            VideoTrack videoTrack = streamModel.getVideoTrack();
            AudioTrack audioTrack = streamModel.getAudioTrack();
            if (audioTrack != null && !streamModel.isAPause()) {
                audioTrack.setVolume(10);
                holder.mic.setImageResource(R.drawable.ic_baseline_mic_24);
            } else {
                holder.mic.setImageResource(R.drawable.ic_baseline_mic_off_24);
            }
            if (videoTrack == null) return;
            String vid = videoTrack.id();
            Log.e(TAG, this.streamModelList.get(position).toString());
            /**
             * 已经 存在 sink,那么先删除，以防两个信息一起冲突了
             */
            if (videoSinkHashMap.containsKey(vid)){
                videoTrack.removeSink(videoSinkHashMap.get(vid));
                videoSinkHashMap.remove(vid);
            }
            if (videoTrack != null && !streamModel.isVPause()) {
                videoSinkHashMap.put(vid, holder.viewRenderer);
                videoTrack.addSink(holder.viewRenderer);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return streamModelList.size();
    }


    public class VideoViewHolder extends RecyclerView.ViewHolder {
        public SurfaceViewRenderer viewRenderer;
        public ImageView mic;
        public String id = null;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            viewRenderer = itemView.findViewById(R.id.video);
            mic = itemView.findViewById(R.id.mic);
            viewRenderer.init(eglBaseContext, null);
        }
    }
}
